package com.fengba.nanzhi.tabbar01;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.Function;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 89311 on 2018/4/11.
 */

public class FXSQAdapter2 extends RecyclerView.Adapter<FXSQAdapter2.LinerViewHolder> {
    private Context context;
    private JSONArray jsonArray;
    private FX_SQ notice;
    private JSONObject job;
    private String type;

    public FXSQAdapter2(Context context, FX_SQ add, String type_) {
        this.context = context;
        this.notice = add;
        this.type = type_;
    }

    @Override
    public LinerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LinerViewHolder(LayoutInflater.from(context).inflate(R.layout.fx_sq_item_view02,parent,false));
    }


    @Override
    public void onBindViewHolder(LinerViewHolder holder, final int position) {

        try {
            job = (JSONObject) jsonArray.get(position);
            Glide.with(context).load(job.get("headimg").toString()).into(holder.SQiv01);//
            holder.SQtv03.setText(Function.phoneHide4Start(job.get("account").toString()));//
            createStart(Integer.parseInt(job.get("level_id").toString()),holder.SQNfl01);
            if (type.equals("2"))
            {
             holder.SQtv08.setText("向她/她赠送");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.SQrl01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    Intent intent = new Intent(notice,FX_SQNumber.class);
                    intent.putExtra("account",((JSONObject) jsonArray.get(position)).get("account").toString());
                    intent.putExtra("headimg",((JSONObject) jsonArray.get(position)).get("headimg").toString());
                    intent.putExtra("level_id",((JSONObject) jsonArray.get(position)).get("level_id").toString());
                    intent.putExtra("giveid",((JSONObject) jsonArray.get(position)).get("id").toString());
                    intent.putExtra("type",type);
                    context.startActivity(intent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });


    }

    @Override
    public int getItemCount() {
        jsonArray = notice.getJsonArray2();
        ////System.out.println("aaaaaa"+jsonArray.toString());
        if(jsonArray!=null) {
            return jsonArray.length();
        }else
            {
                return 0;
            }
    }

    class LinerViewHolder extends RecyclerView.ViewHolder{
        private CircleImageView SQiv01;//
        private FrameLayout SQNfl01;//
        private TextView SQtv03;//
        private RelativeLayout SQrl01;//
        private TextView SQtv08;
        public LinerViewHolder(View itemView) {
            super(itemView);
            SQiv01 = itemView.findViewById(R.id.SQiv01);//
            SQNfl01 = itemView.findViewById(R.id.SQNfl01);//
            SQtv03 = itemView.findViewById(R.id.SQtv03);//
            SQrl01 = itemView.findViewById(R.id.SQrl01);//
            SQtv08 = itemView.findViewById(R.id.SQtv08);//
        }
    }




    /**
     * 生成星星
     * @param index
     */
    private void createStart(int index,FrameLayout SQNfl01_){
        for (int i = 0; i < 4; i++){
            ImageView img = new ImageView(context);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(0,0);
            params.width = Function.dip2px(context,22);
            params.height = Function.dip2px(context,22);
            params.leftMargin = 4+i*Function.dip2px(context,30);
            img.setLayoutParams(params);
            img.setImageDrawable(ContextCompat.getDrawable(context.getApplicationContext(),R.mipmap.img_0022));
            SQNfl01_.addView(img);
        }

        for (int i = 0; i < index; i++){
            ImageView img = new ImageView(context);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(0,0);
            params.width = Function.dip2px(context,22);
            params.height = Function.dip2px(context,22);
            params.leftMargin = 4+i*Function.dip2px(context,30);
            img.setLayoutParams(params);
            img.setImageDrawable(ContextCompat.getDrawable(context.getApplicationContext(),R.mipmap.img_0023));

            SQNfl01_.addView(img);
        }
    }


}
